<?php
// 本文档自动生成，仅供测试运行
class AdminAction extends Action
{
    
    /**
    +----------------------------------------------------------
    * 默认后台地址
    +----------------------------------------------------------
    */
	function _initialize(){
		if( !isset($_SESSION['adm_name']) or !isset($_SESSION['isAdmin']) ) $this->redirect('Index/login');
	}

      /**
    +----------------------------------------------------------
    * 管理员列表
    +----------------------------------------------------------
    */
	function admins(){
	
		import("@.ORG.Page");
		$count	= M('admin')->where("groupid!=6 and groupid!=7")->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$data = M('admin')->where("groupid!=6 and groupid!=7")->order('groupid asc,ctime desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($data as $k=>$v){
			if($v['groupid']==0){
				$data[$k]['gname']="超级管理员";
			}else{
				$gname=M('admin_group')->find($v['groupid']);
				$data[$k]['gname']=$gname['name'];
			}
			if($v['lid']==0){
				$data[$k]['lname']="非翻译";
			}else{
				$lname=M('pz_lang')->find($v['lid']);
				$data[$k]['lname']=$lname['name'];
			}
		}
		$this->assign('data', $data);
		$this->assign('page', $show);
		
		$this->assign('pagetitle', '管理员管理');
		$this->display();
	}

	function editadmin(){
	    $id = $_GET['id'];
		$group_all=M('admin_group')->where("state=1")->order('xu asc,id asc')->select();
	    $this->assign('group_all', $group_all);
		$lang_all=M('pz_lang')->where("state=1")->order('xu asc,id asc')->select();
	    $this->assign('lang_all', $lang_all);

		if($id == ''){
			$this->assign('pagetitle', '添加管理员');
		}else{
            if($id == 1){if($_SESSION['adm_name'] != 'admin')$this->error('您没有修改权限');}
			$f = M('admin')->where('id='.$id)->find();
			if($f){
				$this->assign($f);	
                $this->assign('pagetitle', '编辑管理员');
			}else{
				$this->error('此用户不存在');
			}
		}
		$this->display();
	}
	
	function doeditadmin(){
	
	    if(!M()->autoCheckToken($_POST)) $this->error('非法提交');
		unset($_POST['__hash__']);
		
		if($_POST['pass'] != '' ){
			if( strlen($_POST['pass']) < 6) $this->error('密码过短！');
			if($_POST['pass'] != $_POST['pass2']) $this->error('您两次输入的密码不一致');
			$map['passwd'] = md5( $_POST['pass'] );
		}
		
		$map['lid']=$_POST['lid'];
		$map['state']=$_POST['state']=1;
		$map['ctime']=$_POST['ctime']=time();
		$map['groupid']=$_POST['groupid'];
		
		if($_POST['id']){
		     $map['id'] = $_POST['id'];
			 if($_POST['id'] != 1){
			 	$map['adm_name'] = $_POST['adm_name'];
			 }
             if(M('admin')->save($map)){
		     $this->assign('jumpUrl',U('Admin/admins'));
		     $this->success('修改成功！');
	         }else $this->error('和原始密码一样！');
		}else{
		     $map['id'] = $_POST['id'];
             $map['adm_name'] = $_POST['adm_name'];
             if(M('admin')->add($map)){
		     $this->assign('jumpUrl',U('Admin/admins'));
		     $this->success('添加成功！');
	         }else $this->error('添加失败！');
		}
	
	}
	
	function delAdmin(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);
		echo M('admin')->where($map)->setField('state','3')?'1':'0';   //3为已删除
	}
	function huiAdmin(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);
		echo M('admin')->where($map)->setField('state','1')?'1':'0';
	}
      /**
    +----------------------------------------------------------
    * 会员列表
    +----------------------------------------------------------
    */
	function user(){
	
		import("@.ORG.Page");
		$count	= M('user')->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$data = M('user')->order('groupid asc,ctime desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($data as $k=>$v){
			if(empty($v['tel'])){
				$data[$k]['tel']="无手机";
			}
			if(empty($v['nicheng'])){
				$data[$k]['nicheng']="无昵称";
			}
			if(empty($v['name'])){
				$data[$k]['name']="无姓名";
			}
			if(empty($v['email'])){
				$data[$k]['email']="无邮箱";
			}
			$gname=M('user_group')->find($v['groupid']);
			$data[$k]['gname']=$gname['name'];
		}
		$this->assign('data', $data);
		$this->assign('page', $show);
		
		$this->assign('pagetitle', '会员列表');
		$this->display();
	}

	function edituser(){
	    $id = $_GET['id'];
		$group_all=M('user_group')->where("state=1")->order('xu asc,id asc')->select();
	    $this->assign('group_all', $group_all);

		if($id == ''){
			$this->assign('pagetitle', '添加会员');
		}else{
			$f = M('user')->where('id='.$id)->find();
			$info=M('user_info')->where('uid='.$id)->find();
			$this->assign('info',$info);
			if($f){
				$this->assign($f);	
                $this->assign('pagetitle', '编辑会员');
			}else{
				$this->error('此用户不存在');
			}
		}
		$this->display();
	}
	
	function doedituser(){
	
	    if(!M()->autoCheckToken($_POST)) $this->error('非法提交');
		unset($_POST['__hash__']);
		
		if($_POST['pass'] != '' ){
			if( strlen($_POST['pass']) < 6) $this->error('密码过短！');
			if($_POST['pass'] != $_POST['pass2']) $this->error('您两次输入的密码不一致');
			$map['passwd'] = md5( $_POST['pass'] );
		}
				
		$map['groupid']=$_POST['groupid'];
		
		if($_POST['id']){
			$map['id'] = $_POST['id'];
			$map['state']=$_POST['state']=1;
			$map['stime']=$_POST['stime']=time();
			$map['name'] = $_POST['name'];
			$map['nicheng'] = $_POST['nicheng'];
			$map['touxiang'] = $_POST['touxiang'];
			$map['passwd'] = $_POST['passwd'];
			$map['groupid'] = $_POST['groupid'];
			$map['tel'] = $_POST['tel'];
			$map['email'] = $_POST['email'];
			
			$map_info['uid']=$_POST['id'];
			$map_info['sex']=$_POST['sex'];
			$map_info['yiming']=$_POST['yiming'];
			$map_info['year']=$_POST['year'];
			$map_info['month']=$_POST['month'];
			$map_info['day']=$_POST['day'];
			$map_info['home']=$_POST['home'];
			$map_info['minzu']=$_POST['minzu'];
			$map_info['shengao']=$_POST['shengao'];
			$map_info['tizhong']=$_POST['tizhong'];
			$map_info['xingzuo']=$_POST['xingzuo'];
			$map_info['yuyan']=$_POST['yuyan'];
			$map_info['area']=$_POST['area'];
			$map_info['guhua']=$_POST['guhua'];
			$map_info['xingqu']=$_POST['xingqu'];
			$map_info['peixun']=$_POST['peixun'];
			$map_info['jianjie']=$_POST['jianjie'];
			
			$map_info_jiu=M('user_info')->where("uid=".$_POST['id']."")->find();
			if(empty($map_info_jiu)){
				$user_info_save=M('user_info')->add($map_info);
			}else{
				$user_info_save=M('user_info')->save($map_info);
			}
			$user_save=M('user')->save($map);
			
			if($user_save || $user_info_save ){
				$this->assign('jumpUrl',U('Admin/user'));
				$this->success('修改成功！');
			}else $this->error('修改失败！');
			
		}else{
			$map['state']=$_POST['state']=1;
			$map['ctime']=$_POST['ctime']=time();
			$map['name'] = $_POST['name'];
			$map['nicheng'] = $_POST['nicheng'];
			$map['touxiang'] = $_POST['touxiang'];
			$map['passwd'] = $_POST['passwd'];
			$map['groupid'] = $_POST['groupid'];
			$map['tel'] = $_POST['tel'];
			$map['email'] = $_POST['email'];
			

			$user_add=M('user')->add($map);
			
			$map_info['uid']=$user_add;
			$map_info['sex']=$_POST['sex'];
			$map_info['yiming']=$_POST['yiming'];
			$map_info['year']=$_POST['year'];
			$map_info['month']=$_POST['month'];
			$map_info['day']=$_POST['day'];
			$map_info['home']=$_POST['home'];
			$map_info['minzu']=$_POST['minzu'];
			$map_info['shengao']=$_POST['shengao'];
			$map_info['tizhong']=$_POST['tizhong'];
			$map_info['xingzuo']=$_POST['xingzuo'];
			$map_info['yuyan']=$_POST['yuyan'];
			$map_info['area']=$_POST['area'];
			$map_info['guhua']=$_POST['guhua'];
			$map_info['xingqu']=$_POST['xingqu'];
			$map_info['peixun']=$_POST['peixun'];
			$map_info['jianjie']=$_POST['jianjie'];
			$user_info_add=M('user_info')->add($map_info);
			
			if($user_add || $user_info_add ){
				$this->assign('jumpUrl',U('Admin/user'));
				$this->success('添加成功！');
			}else $this->error('添加失败！');
		}
	
	}
	function deluser(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);
		echo M('user')->where($map)->setField('state','3')?'1':'0';   //3为已删除
	}
	function huiuser(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);
		echo M('user')->where($map)->setField('state','1')?'1':'0';
	}
      /**
    +----------------------------------------------------------
    * 会员列表
    +----------------------------------------------------------
    */
	function user_group(){
	
		import("@.ORG.Page");
		$count	= M('user_group')->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$data = M('user_group')->order('xu asc,ctime desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('data', $data);
		$this->assign('page', $show);
		
		$this->assign('pagetitle', '会员组列表');
		$this->display();
	}

	function edituser_group(){
	    $id = $_GET['id'];

		if($id == ''){
			$this->assign('pagetitle', '添加会员组');
		}else{
			$f = M('user_group')->where('id='.$id)->find();
			if($f){
				$this->assign($f);	
                $this->assign('pagetitle', '编辑会员组');
			}else{
				$this->error('此用户不存在');
			}
		}
		$this->display();
	}
	
	function doedituser_group(){
	
	    if(!M()->autoCheckToken($_POST)) $this->error('非法提交');
		unset($_POST['__hash__']);
		
		
		if($_POST['id']){
			$map['id'] = $_POST['id'];
			$map['state']=$_POST['state']=1;
			$map['stime']=$_POST['stime']=time();
			$map['name'] = $_POST['name'];
			$map['xu'] = $_POST['xu'];
			if(M('user_group')->save($map)){
				$this->assign('jumpUrl',U('Admin/user_group'));
				$this->success('修改成功！');
			}else $this->error('和原始密码一样！');
		}else{
			$map['state']=$_POST['state']=1;
			$map['ctime']=$_POST['ctime']=time();
			$map['name'] = $_POST['name'];
			$map['xu'] = $_POST['xu'];
			if(M('user_group')->add($map)){
				$this->assign('jumpUrl',U('Admin/user_group'));
				$this->success('添加成功！');
			}else $this->error('添加失败！');
		}
	
	}
	function deluser_group(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);
		echo M('user_group')->where($map)->setField('state','3')?'1':'0';   //3为已删除
	}
	function huiuser_group(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);
		echo M('user_group')->where($map)->setField('state','1')?'1':'0';
	}
      /**
    +----------------------------------------------------------
    * 文章评论
    +----------------------------------------------------------
    */
	function article_pl(){
	
		import("@.ORG.Page");
		$count	= M('article_pl')->where("plid=0")->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$data = M('article_pl')->where("plid=0")->order('zan desc,cai desc,ctime desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($data as $k=>$v){
			$admin=M("admin")->find($v['uid']);
			if(!empty($admin['nicheng'])){
				$data[$k]['username']=$admin['nicheng'];
			}else{
				$data[$k]['username']=$admin['adm_name'];
			}
			$usergrp=M("admin_group")->find($admin['groupid']);
			$data[$k]['usergrp']=$usergrp['name'];
			
			$artname=M("article")->find($v['artid']);
			$data[$k]['artname']=$artname['name'];
			
			$data[$k]['hf']=M('article_pl')->where("artid=".$v['artid']." and plid=".$v['id']." ")->order('ctime asc,id asc')->select();
			foreach($data[$k]['hf'] as $kk=>$vv){
				
				$zuozhe_hf=M('admin')->find($vv['uid']);
				if(!empty($zuozhe_hf['nicheng'])){
					$data[$k]['hf'][$kk]['zuozhe_nc']=$zuozhe_hf['nicheng'];
				}else{
					$data[$k]['hf'][$kk]['zuozhe_nc']=$zuozhe_hf['adm_name'];
				}
				
				$usergrp=M("admin_group")->find($zuozhe_hf['groupid']);
				$data[$k]['hf'][$kk]['usergrp']=$usergrp['name'];
				
				$duixiang_hf=M('admin')->find($vv['duid']);
				if(!empty($duixiang_hf['nicheng'])){
					$data[$k]['hf'][$kk]['duixiang_nc']=$duixiang_hf['nicheng'];
				}else{
					$data[$k]['hf'][$kk]['duixiang_nc']=$duixiang_hf['adm_name'];
				}
			}
			

		}
		$this->assign('data', $data);
		$this->assign('page', $show);
		
		$this->assign('pagetitle', '评论列表');
		$this->display();
	}

	function editarticle_pl(){
	    $id = $_GET['id'];

		if($id == ''){
			$this->assign('pagetitle', '添加评论');
		}else{
            if($id == 1){if($_SESSION['adm_name'] != 'admin')$this->error('您没有修改权限');}
			$f = M('article_pl')->where('id='.$id)->find();
			if($f){
	
				$admin=M("admin")->find($f['uid']);
				if(!empty($admin['nicheng'])){
					$f['username']=$admin['nicheng'];
				}else{
					$f['username']=$admin['adm_name'];
				}
				$usergrp=M("admin_group")->find($admin['groupid']);
				$f['usergrp']=$usergrp['name'];
				
				$artname=M("article")->find($f['artid']);
				$f['artname']=$artname['name'];
				
				if($f['duid']!=0){
					$duixiang_hf=M('admin')->find($f['duid']);
					if(!empty($duixiang_hf['nicheng'])){
						$f['duixiang_nc']=$duixiang_hf['nicheng'];
					}else{
						$f['duixiang_nc']=$duixiang_hf['adm_name'];
					}
				}else{
					$f['duixiang_nc']="无";
				}
				
				$this->assign($f);	
                $this->assign('pagetitle', '编辑评论');
			}else{
				$this->error('此评论不存在');
			}
		}
		$this->display();
	}
	
	function doeditarticle_pl(){
	
	    if(!M()->autoCheckToken($_POST)) $this->error('非法提交');
		unset($_POST['__hash__']);
		
		if($_POST['pass'] != '' ){
			if( strlen($_POST['pass']) < 6) $this->error('密码过短！');
			if($_POST['pass'] != $_POST['pass2']) $this->error('您两次输入的密码不一致');
			$map['passwd'] = md5( $_POST['pass'] );
		}
				
		if($_POST['id']){
		     $map['id'] = $_POST['id'];
		     $map['content'] = $_POST['content'];
             if(M('article_pl')->save($map)){
		     $this->assign('jumpUrl',U('Admin/article_pl'));
		     $this->success('修改成功！');
	         }else $this->error('和原始密码一样！');
		}else{
			$map['state']=$_POST['state']=1;
			$map['ctime']=$_POST['ctime']=time();
			$map['artid'] = $_POST['artid'];
			$map['uid'] = $_SESSION['uid'];
			$map['content'] = $_POST['content'];
			if(M('article_pl')->add($map)){
				$this->assign('jumpUrl',U('Admin/article_pl'));
				$this->success('添加成功！');
			}else $this->error('添加失败！');
		}
	
	}
	function delarticle_pl(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);

		echo M('article_pl')->where($map)->setField('state','3')?'1':'0';   //3为已删除
	}
	function huiarticle_pl(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);
		echo M('article_pl')->where($map)->setField('state','1')?'1':'0';
	}
      /**
    +----------------------------------------------------------
    * 商家评论
    +----------------------------------------------------------
    */
	function shop_pl(){
	
		import("@.ORG.Page");
		$count	= M('shop_pl')->where("plid=0")->count();
		$Page	= New Page($count,15);
		$show	= $Page->show();
		$data = M('shop_pl')->where("plid=0")->order('zan desc,cai desc,ctime desc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($data as $k=>$v){
			$admin=M("admin")->find($v['uid']);
			if(!empty($admin['nicheng'])){
				$data[$k]['username']=$admin['nicheng'];
			}else{
				$data[$k]['username']=$admin['adm_name'];
			}
			$usergrp=M("admin_group")->find($admin['groupid']);
			$data[$k]['usergrp']=$usergrp['name'];
			
			$artname=M("shop")->find($v['artid']);
			$data[$k]['artname']=$artname['name'];
			
			$data[$k]['hf']=M('shop_pl')->where("artid=".$v['artid']." and plid=".$v['id']." ")->order('ctime asc,id asc')->select();
			foreach($data[$k]['hf'] as $kk=>$vv){
				
				$zuozhe_hf=M('admin')->find($vv['uid']);
				if(!empty($zuozhe_hf['nicheng'])){
					$data[$k]['hf'][$kk]['zuozhe_nc']=$zuozhe_hf['nicheng'];
				}else{
					$data[$k]['hf'][$kk]['zuozhe_nc']=$zuozhe_hf['adm_name'];
				}
				
				$usergrp=M("admin_group")->find($zuozhe_hf['groupid']);
				$data[$k]['hf'][$kk]['usergrp']=$usergrp['name'];
				
				$duixiang_hf=M('admin')->find($vv['duid']);
				if(!empty($duixiang_hf['nicheng'])){
					$data[$k]['hf'][$kk]['duixiang_nc']=$duixiang_hf['nicheng'];
				}else{
					$data[$k]['hf'][$kk]['duixiang_nc']=$duixiang_hf['adm_name'];
				}
			}
			

		}
		$this->assign('data', $data);
		$this->assign('page', $show);
		
		$this->assign('pagetitle', '评论列表');
		$this->display();
	}

	function editshop_pl(){
	    $id = $_GET['id'];

		if($id == ''){
			$this->assign('pagetitle', '添加评论');
		}else{
            if($id == 1){if($_SESSION['adm_name'] != 'admin')$this->error('您没有修改权限');}
			$f = M('shop_pl')->where('id='.$id)->find();
			if($f){
	
				$admin=M("admin")->find($f['uid']);
				if(!empty($admin['nicheng'])){
					$f['username']=$admin['nicheng'];
				}else{
					$f['username']=$admin['adm_name'];
				}
				$usergrp=M("admin_group")->find($admin['groupid']);
				$f['usergrp']=$usergrp['name'];
				
				$artname=M("shop")->find($f['artid']);
				$f['artname']=$artname['name'];
				
				if($f['duid']!=0){
					$duixiang_hf=M('admin')->find($f['duid']);
					if(!empty($duixiang_hf['nicheng'])){
						$f['duixiang_nc']=$duixiang_hf['nicheng'];
					}else{
						$f['duixiang_nc']=$duixiang_hf['adm_name'];
					}
				}else{
					$f['duixiang_nc']="无";
				}
				
				$this->assign($f);	
                $this->assign('pagetitle', '编辑评论');
			}else{
				$this->error('此评论不存在');
			}
		}
		$this->display();
	}
	
	function doeditshop_pl(){
	
	    if(!M()->autoCheckToken($_POST)) $this->error('非法提交');
		unset($_POST['__hash__']);
		
		if($_POST['pass'] != '' ){
			if( strlen($_POST['pass']) < 6) $this->error('密码过短！');
			if($_POST['pass'] != $_POST['pass2']) $this->error('您两次输入的密码不一致');
			$map['passwd'] = md5( $_POST['pass'] );
		}
				
		if($_POST['id']){
		     $map['id'] = $_POST['id'];
		     $map['content'] = $_POST['content'];
             if(M('shop_pl')->save($map)){
		     $this->assign('jumpUrl',U('Admin/shop_pl'));
		     $this->success('修改成功！');
	         }else $this->error('和原始密码一样！');
		}else{
			$map['state']=$_POST['state']=1;
			$map['ctime']=$_POST['ctime']=time();
			$map['artid'] = $_POST['artid'];
			$map['uid'] = $_SESSION['uid'];
			$map['content'] = $_POST['content'];
			if(M('shop_pl')->add($map)){
				$this->assign('jumpUrl',U('Admin/shop_pl'));
				$this->success('添加成功！');
			}else $this->error('添加失败！');
		}
	
	}
	function delshop_pl(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);

		echo M('shop_pl')->where($map)->setField('state','3')?'1':'0';   //3为已删除
	}
	function huishop_pl(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);
		echo M('shop_pl')->where($map)->setField('state','1')?'1':'0';
	}
	

}
?>